/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2015
 */

package com.environment_pro.monitor.service.impl;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.environment_pro.monitor.mapper.TbDeviceMsgMapper;
import com.environment_pro.monitor.model.TbDeviceMsg;
import com.environment_pro.monitor.service.TbDeviceMsgService;
import com.environment_pro.monitor.util.PageHelper;
import com.environment_pro.monitor.util.PageInfo;
import com.environment_pro.monitor.util.PageHelper.Page;
import com.environment_pro.monitor.util.StaticUtil;

/**
 * @author yangfeng 
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional  //此处不再进行创建SqlSession和提交事务，都已交由spring去管理了。
public class TbDeviceMsgServiceImpl implements TbDeviceMsgService {
	
	@Resource
	private TbDeviceMsgMapper mapper;

	//移除缓存
	//@CacheEvict(value = "tbDeviceMsgCache",key="'id'+#id")
	public boolean delete(int id) {
		
		return mapper.delete(id);
	}

	@SuppressWarnings("unchecked")
	public Page<TbDeviceMsg> findAll(PageInfo pageInfo, TbDeviceMsg tbDeviceMsg) {
		PageHelper.startPage(pageInfo);  
		mapper.findAll(tbDeviceMsg);
		return PageHelper.endPage();
	}

	//使用缓存
	//@Cacheable(value = "tbDeviceMsgCache",key="'id'+#id")
	public TbDeviceMsg findById(int id) {
		TbDeviceMsg tbDeviceMsg = mapper.findById(id);
		return tbDeviceMsg;
	}
	
	//缓存数据
	//@CachePut(value = "tbDeviceMsgCache",key="'id'+#tbDeviceMsg.id")
	public void save(TbDeviceMsg tbDeviceMsg) {
		tbDeviceMsg.setAddTime(new Date());
		mapper.save(tbDeviceMsg);
	}
	//更新缓存
	//@CacheEvict(value = "tbDeviceMsgCache",key="'id'+#tbDeviceMsg.id")
	public boolean update(TbDeviceMsg tbDeviceMsg) {

		return mapper.update(tbDeviceMsg);
	}
	
	/**
	 * 根据socket消息，解析消息入库
	 */
	public void save(String message) {
		TbDeviceMsg tbDeviceMsg = new TbDeviceMsg();
		tbDeviceMsg.setMsgFrameContent(message);
		tbDeviceMsg.setAddTime(new Date());
		mapper.save(tbDeviceMsg);
	}
	
	public void backupData(int duration) {
		String backupTime = StaticUtil.getDateString((0-duration), "yyyy-MM-dd");
		mapper.backupInsert(backupTime);
		mapper.backupDelete(backupTime);
	}

	//编码转化为名称
	public String code2name(String code) {
		// TODO Auto-generated method stub
		return null;
	}

	//ID转化为名称
	public String id2name(int id) {
		
		return null;
	}

}